﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using ShanjinPDAUI.ModelEntity;
namespace ShanjinPDAUI.Data
{
    public class UserinfoServer
    {
        private IHttpClientFactory _httpClientFactory { get; set; }
        public UserinfoServer(IHttpClientFactory httpClientFactory) 
        {
            _httpClientFactory = httpClientFactory;
        }
        public async Task<bool> login(string username ,string pwd) 
        {
            using (HttpClientHandler clientHandler = new HttpClientHandler())
            {
                clientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, error) => true;
                using (HttpClient client = new HttpClient(clientHandler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Post,$"{AppSettingsJson.GetAppSettings().GetSection("ConnectionUrl")["DefaultConnection"]}/api/UserInfo");
                    request.Headers.Add("Accept", "text / plain");
                    Usertemp usertemp = new Usertemp()
                    {
                        UserName = username,
                        Pwd = pwd,
                    };
                    request.Content = new StringContent(JsonSerializer.Serialize<Usertemp>(usertemp), Encoding.UTF8, "application/json");
                    var response = await client.SendAsync(request);
                    try
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            using var responseStream = await response.Content.ReadAsStreamAsync();
                            var user = await JsonSerializer.DeserializeAsync
                                <UserInfoEntity>(responseStream);
                            if (user.Result)
                            {
                                return true;
                            }
                            else
                            {
                                return false;
                            }
                        }
                        else
                        {
                            return false;
                        }
                    }
                    catch (Exception e)
                    {

                        return false;
                    }
                   
                }
            }
        }
    }
    public class Usertemp
    {
        public string UserName { get; set; }
        public string Pwd { get; set; }
    }

}
